package main

import (
	"fmt"
	"sync"
)

// main
//
//	@Description: mutex 互斥锁
func main() {
	commonUse()

}
func commonUse() {
	var lock sync.Mutex
	var count int
	// 辅助变量，用来确认所有的goroutine都完成
	var wg sync.WaitGroup

	for i := 0; i < 100; i++ {
		wg.Add(1)
		go func() {

			defer wg.Done()
			for i := 0; i < 10000; i++ {
				lock.Lock()
				count++
				lock.Unlock()
			}
		}()
	}
	wg.Wait()
	fmt.Println("count=", count)
}

// CounterOne
// @Description: 使用1
type CounterOne struct {
	mu    sync.Mutex
	Count uint64
}

// CounterTwo
// @Description: 使用2
type CounterTwo struct {
	sync.Mutex
	Count uint64
}

type Foo struct {
	mu    sync.Mutex
	Count uint64
}

func (f *Foo) Bar() {
	f.mu.Lock()
	defer f.mu.Unlock()

	f.Count++
	//do something
}
